package servlet;

import bean.Role;
import bean.User;
import dao.HibernateRoleDao;
import dao.HibernateUserDao;
import interfaces.RoleDao;
import interfaces.UserDao;
import java.io.IOException;
import java.sql.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author yakimovich
 */
public class EditUser extends HttpServlet {

    private RequestDispatcher dispatcher;
    private static org.apache.commons.logging.Log logger 
            = LogFactory.getLog(EditUser.class);

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        String login = request.getParameter("login");
        HttpSession session = request.getSession();
        UserDao userDao = new HibernateUserDao();

        User user = null;
        user = userDao.findByLogin(login);
        if (user == null) {
            response.sendRedirect("/login.jsp");
            session.invalidate();
        } else {
            request.setAttribute("user", user);
            dispatcher = getServletContext()
                    .getRequestDispatcher("/editUser.jsp");
            dispatcher.forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {

        UserDao userDao = new HibernateUserDao();

        long id = Long.parseLong(request.getParameter("id"));
        String login = request.getParameter("login");
        String password = request.getParameter("password");
        String passwordAgain = request.getParameter("passwordagain");
        String email = request.getParameter("email");
        String firstName = request.getParameter("firstname");
        String lastName = request.getParameter("lastname");
        String birhday = request.getParameter("birth");
        String role = request.getParameter("role");
        Date birthday = null;
        try {
            birthday = Date.valueOf(request.getParameter("birth"));
        } catch (IllegalArgumentException e) {
            logger.error(this, e);
            request.setAttribute("error", "Wrong date format");
            dispatcher = getServletContext()
                    .getRequestDispatcher("/editUser.jsp");
            dispatcher.forward(request, response);
        }

        if (!(request.getParameter("password")
                .equals(request.getParameter("passwordagain")))) {
            request.setAttribute("error", "passwords not equal");
            dispatcher = getServletContext()
                    .getRequestDispatcher("/editUser.jsp");
            dispatcher.forward(request, response);
            return;
        }

        User user = new User();
        user.setId(id);
        user.setLogin(login);
        user.setPassword(password);
        user.setEmail(email);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        user.setBirthday(birthday);

        RoleDao roleDao = new HibernateRoleDao();
        Role roleUser = roleDao.findByName(role);
        user.setRole(roleUser);

        userDao.update(user);

        List<User> list = userDao.findAll();
        request.setAttribute("users", list);
        dispatcher = getServletContext()
                .getRequestDispatcher("/adminPage.jsp");
        dispatcher.forward(request, response);
    }
}
